﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Citi.HeadCount.Domain;
using Citi.HeadCount.Domain.Repository;
using Citi.HeadCount.WebUI.Models;

namespace Citi.HeadCount.WebUI.Controllers
{
    public class GOCController : Controller
    {
        private GOCRepository gocRepository { get; set; }

        public GOCController()
        {
            gocRepository = new GOCRepository();
        }

        public List<GOC> MountListGOCsException()
        {
            //var referenceDate = new DateTime(2013, 04, 01);
            return gocRepository.GetGOCsRuleException().Take(300).ToList();
        }

        public ActionResult ShowGOCsException()
        {
            var model = GOCAttributesViewModel.CreateModel(MountListGOCsException());
            TempData["Exception"] = true;
            TempData["GOCIndexModel"] = model;
            return RedirectToAction("Index");
        }

        public void SetRuleResult(int id, int ruleResultId, bool applyToAll)
        {
            var result = new RuleResultRepository().GetById(ruleResultId);

            gocRepository = new GOCRepository();
            var goc = gocRepository.GetById(id);
            goc.RuleResultId = ruleResultId;
            goc.RuleResult = result;
            gocRepository.Save(goc);

            new RuleResultRepository().SetAsException(ruleResultId);
            //gocRepository.SaveRuleResultIdException(id, ruleResultId);
            if (applyToAll)
                new RuleResultController().ApplyException(result);
        }

        public ActionResult Index()
        {
            if (TempData["GOCIndexModel"] == null)
                return RedirectToAction("Index", "Home");

            return View(TempData["GOCIndexModel"]  as IEnumerable<GOCAttributesViewModel>);
        }
    }
}
